/*
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Authors:
 *  	Albert Marieges Garcia
 *  	Álex Pardo Fernández
 *  	Enric Mayas Márquez
 *  	Juan Carlos Calvo Tejedor
 *  	Marc Gironés Dezsényi
 *  
 *  Contact:
 *  	bcn.tours.android@gmail.com
 *  Webpage:
 *  	bcntours.co.cc
 *
 */

package com.BCNTours.AR.Utils;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;


public class ARLocationManager{
	
	private LocationManager myLocationManager; 
	private String provider;
	
	/**
	 * Creates a new location manager
	 * @param appContext
	 */
	public ARLocationManager(Context appContext){
	    myLocationManager = (LocationManager)appContext.getSystemService(Context.LOCATION_SERVICE);

	    Criteria criteria = new Criteria();
	    criteria.setAccuracy(Criteria.ACCURACY_FINE);
	    criteria.setAltitudeRequired(false);
	    criteria.setBearingRequired(false);
	    criteria.setCostAllowed(true);
	    criteria.setPowerRequirement(Criteria.POWER_LOW );
	    provider = myLocationManager.getBestProvider(criteria, true);

	}
	
	/**
	 * Request automatic updates when the time or the distance arrives to the defined value
	 * @param maxTime
	 * @param maxDist
	 * @param listener
	 */
	public void requestUpdates(int maxTime,int maxDist, LocationListener listener){
		myLocationManager.requestLocationUpdates(provider, maxTime, maxDist, listener);
	}
	
	/**
	 * Returns the last location registered by the location service
	 * @return
	 */
	public Location getLastLocation(){
		if(provider != null){
			return myLocationManager.getLastKnownLocation(provider);
		} else{
			return null;
		}
		
	}
	
	/**
	 * Removes the location request of automatic updates
	 * @param listener
	 */
	public void removeUpdates(LocationListener listener){
		myLocationManager.removeUpdates(listener);
	}
}
